import axiosIns from './axiosIns'
import timeout from './error.js'
import url from './config'
const errMsg = 'tomcat原地爆炸了'

export default {
  getData (params) {
    return new Promise((resolve, reject) => {
      if (window.localStorage.root === '1') {
        axiosIns.get(`${url}/news/list/${params}/10`)
        .then(data => {
          if (data.data.data.success === true) {
            resolve(data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      } else {
        axiosIns.get(`${url}/user/list/news/${params}/10`, {
          withCredentials: true,
          xsrfCookieName: 'token'
        })
        .then(data => {
          if (data.data.data.success === true) {
            resolve(data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      }

      timeout(reject)
    })
  },
  getDetail (params) {
    return new Promise((resolve, reject) => {
      axiosIns.get(`${url}/news/detail/${params}`)
      .then(data => {
        if (data.data.success === true) {
          resolve(data.data)
        } else {
          reject(data.message)
        }
      })
      .catch(() => {
        reject(errMsg)
      })
      timeout(reject)
    })
  },
  deleteData (params) {
    return new Promise((resolve, reject) => {
      axiosIns.delete(`${url}/news/delete/${params}`)
        .then(data => {
          console.log(data)
          if (data.data.data.success === true) {
            resolve(data.data.data.message)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      timeout(reject)
    })
  },
  review (params) {
    return new Promise((resolve, reject) => {
      axiosIns.get(`${url}/news/detail/${params}`)
        .then(data => {
          if (data.data.success === true) {
            resolve(data.data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      timeout(reject)
    })
  },
  examine (params) {
    return new Promise((resolve, reject) => {
      axiosIns.post(`${url}/user/newsReview`, params)
        .then(data => {
          if (data.data.data.success) {
            resolve(data.data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          console.log('err')
          reject(errMsg)
        })
      timeout(reject)
    })
  },
  editNews (params) {
    return new Promise((resolve, reject) => {
      axiosIns.put(`${url}/news/update`, params)
        .then(data => {
          if (data.data.data.success) {
            resolve()
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      timeout(reject)
    })
  },
  searchData (params) {
    return new Promise((resolve, reject) => {
      console.log(params)
      let page = params.page || 1
      axiosIns.post(`${url}/news/list/${page}/10`, {tagsNames: params.tagName})
        .then(data => {
          if (data.data.data.success) {
            resolve(data.data)
          } else {
            reject(data.data.data.message)
          }
        })
      timeout(reject)
    })
  }
}
